package com.keur.money.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.keur.money.R
import com.keur.money.app.MyApp
import com.keur.money.base.utils.thread.TimingThread
import com.keur.money.databinding.DialogMullaUserDataClearBinding
import com.keur.money.http.HttpHelper
import com.keur.money.utils.event.KeurEventType

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/8 15:50
 * @Annotation：清空用户数据dialog
 */
class UserDataClearDialog: ProDialog, TimingThread.ITimingThreadListener {

    private lateinit var binding: DialogMullaUserDataClearBinding
    private var mCallback: IDialogCallback? = null

    //倒计时
    private var countdown = 10
    private var timeThread: TimingThread? = null

    override fun initWindow() {
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.picker_view_slide_anim) //修改动画样式
        window?.setGravity(Gravity.BOTTOM)
    }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogMullaUserDataClearBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {

    }

    override fun initListener() {
        setOnClickListener(binding.parentLayout)
        setOnClickListener(binding.contentLayout)
        setOnClickListener(binding.btnReturn)
        setOnClickListener(binding.btnConfirm)
    }

    override fun initData() {

    }

    override fun onViewClick(v: View?) {
        if (v?.id == R.id.contentLayout){
            return
        }
        dismiss()
        when(v?.id){
            R.id.btnConfirm -> {
                mCallback?.onMullaConfirmClick()
            }
        }
    }

    interface IDialogCallback {
        //按钮点击
        fun onMullaConfirmClick()
    }

    /**
     * 启动倒计时
     */
    @SuppressLint("SetTextI18n")
    private fun startTimeThread() {
        if (timeThread != null) {
            timeThread?.stopThread()
        }
        countdown = 10
        checkMullaConfirmStatus()
        if (timeThread == null) {
            timeThread = TimingThread()
            timeThread?.setThreadDelayMillis(1000)
            timeThread?.setLoadListener(this)
        }
        timeThread?.startThread()
    }

    override fun onLoading() {
        if (countdown <= 0) {
            checkMullaConfirmStatus()
            countdown = 10
            timeThread?.stopThread()
            return
        }
        countdown--
        checkMullaConfirmStatus()
    }

    @SuppressLint("SetTextI18n")
    private fun checkMullaConfirmStatus(){
        if (countdown <= 0){
            binding.btnConfirm.isEnabled = true
            binding.btnConfirm.text = MyApp.app.getString(R.string.btn_confirm)
            binding.btnConfirm.setBackgroundResource(R.drawable.mulla_radius_fill_ff8027ff_12)
        } else {
            binding.btnConfirm.isEnabled = false
            binding.btnConfirm.text = "${MyApp.app.getString(R.string.btn_confirm)}(${countdown}s)"
            binding.btnConfirm.setBackgroundResource(R.drawable.mulla_radius_fill_ff999999_12)
        }
    }

    override fun onStart() {
        super.onStart()
        startTimeThread()
        HttpHelper.addKeurEventAction(KeurEventType.DIALOG.KEUR_DIALOG_USER_DATA_CLEAR_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        if (timeThread != null) {
            timeThread?.stopThread()
            timeThread = null
        }
        HttpHelper.addKeurEventAction(KeurEventType.DIALOG.KEUR_DIALOG_USER_DATA_CLEAR_DISMISS)
    }
}